package com.nuoniu.sibanyun.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.finance.InSubsidiary;
import com.nuoniu.sibanyun.entity.finance.InSummary;
import com.nuoniu.sibanyun.mapper.finance.InSummaryMapper;
import com.nuoniu.sibanyun.mapper.inOut.VInOutRecordMainMapper;
import com.nuoniu.sibanyun.service.finance.IInSubsidiaryService;
import com.nuoniu.sibanyun.service.finance.IInSummaryService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 存货总账表 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-05-26
 */
@Service
public class InSummaryServiceImpl extends ServiceImpl<InSummaryMapper, InSummary> implements IInSummaryService {

    @Autowired
    private IInSubsidiaryService subsidiaryService;
    @Autowired
    private VInOutRecordMainMapper inOutRecordMainMapper;

    @Override
    @Transactional
    public void inventoryAccounting(List<InSubsidiary> inSubsidiaries) {
        Map<Integer, List<InSummary>> summaryMap = new HashMap<>();
        subsidiaryService.saveBatch(inSubsidiaries);

        //计入存货总账
        for(InSubsidiary subsidiary : inSubsidiaries) {
            List<InSummary> summarys = baseMapper.getSummaryList(subsidiary.getInventoryId(), subsidiary.getCompanyId(), subsidiary.getAccountYear());
            if(summarys.size() > 0) {
                summaryMap.put(subsidiary.getInventoryId(), summarys);
            }

            if(summaryMap.get(subsidiary.getInventoryId()) == null) {
                List<InSummary> summaryList = new ArrayList<>();
                //获取期初
                Map<String, BigDecimal> beginMap = inOutRecordMainMapper.getBeginMap(subsidiary.getInventoryId(), subsidiary.getAccountId());
                //初始化所有期间
                for(int i=1; i<13; i++) {
                    InSummary summary = new InSummary();
                    summary.setAccountYear(subsidiary.getAccountYear());
                    summary.setAccountPeriod(i);
                    summary.setCompanyId(subsidiary.getCompanyId());
                    summary.setBalanceQuantity(new BigDecimal(0.0));
                    summary.setBalanceAmount(new BigDecimal(0.0));
                    summary.setInventoryId(subsidiary.getInventoryId());
                    summary.setInventoryName(subsidiary.getInventoryName());
                    summary.setInventoryCode(subsidiary.getInventoryCode());

                    if(beginMap != null) {
                        summary.setBalanceAmount(beginMap.get("amount"));
                        summary.setBalanceQuantity(beginMap.get("quantity"));
                    }

                    if(subsidiary.getRdFlag().equals(1)) { //收
                        summary.setInAmount(subsidiary.getInAmount());
                        summary.setInQuantity(subsidiary.getInQuantity());
                        if(summary.getAccountPeriod() >= i) {
                            summary.setBalanceQuantity(summary.getBalanceQuantity().add(subsidiary.getInQuantity()));
                            summary.setBalanceAmount(summary.getBalanceAmount().add(subsidiary.getInAmount()));
                        }
                    } else { //发
                        summary.setOutAmount(subsidiary.getOutAmount());
                        summary.setOutQuantity(subsidiary.getOutQuantity());
                        if(summary.getAccountPeriod() >= i) {
                            summary.setBalanceQuantity(summary.getBalanceQuantity().subtract(subsidiary.getInQuantity()));
                            summary.setBalanceAmount(summary.getBalanceAmount().subtract(subsidiary.getInAmount()));
                        }
                    }

                    summaryList.add(summary);
                }

                summaryMap.put(subsidiary.getInventoryId(), summaryList);
            }else {
                List<InSummary> summaryList = summaryMap.get(subsidiary.getInventoryId());
                for(InSummary summary : summaryList) {
                    if(summary.getAccountPeriod() >= subsidiary.getAccountPeriod()) {
                        if(subsidiary.getRdFlag().equals(1)) { //收
                            summary.setInAmount(summary.getInAmount().add(subsidiary.getInAmount()));
                            summary.setInQuantity(summary.getInQuantity().add(subsidiary.getInQuantity()));
                            summary.setBalanceQuantity(summary.getBalanceQuantity().add(subsidiary.getInQuantity()));
                            summary.setBalanceAmount(summary.getBalanceAmount().add(subsidiary.getInAmount()));
                        } else { //发
                            summary.setOutAmount(subsidiary.getOutAmount());
                            summary.setOutQuantity(subsidiary.getOutQuantity());
                            summary.setBalanceQuantity(summary.getBalanceQuantity().subtract(subsidiary.getInQuantity()));
                            summary.setBalanceAmount(summary.getBalanceAmount().subtract(subsidiary.getInAmount()));
                        }
                    }
                }

                summaryMap.put(subsidiary.getInventoryId(), summaryList);
            }

            for(Integer key : summaryMap.keySet()) {
                this.updateBatchById(summaryMap.get(key));
            }
        }
    }

    @Override
    public InSummary getSummaryInfo(int accountYear, int accountPeriod, Integer inventoryId, Integer companyId, Integer accountId) {
        if(accountPeriod != 0) {
            InSummary summary = baseMapper.getDetail(accountYear, accountPeriod, inventoryId, companyId);
            return summary;
        }

        //获取期初
        Map<String, BigDecimal> beginMap = inOutRecordMainMapper.getBeginMap(inventoryId, accountId);
        InSummary summary = new InSummary();
        if(beginMap != null) {
            summary.setBalanceAmount(beginMap.get("amount"));
            summary.setBalanceQuantity(beginMap.get("quantity"));
        }
        return summary;
    }

    @Override
    public Page<InSummary> pageSummaryList(Page page, Integer inventoryId, Integer accountYear, Integer accountPeriod) {
        QueryWrapper wrapper = new QueryWrapper();
        UserInfo userInfo = UserThreadLocal.get();
        wrapper.eq("account_year", accountYear);
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.eq("account_period", accountPeriod);
        if(inventoryId != null) {
            wrapper.eq("inventory_id", inventoryId);
        }

        return page(page, wrapper);
    }
}
